package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.OverFlowDao;
import com.atguigu.jxc.dao.PurchaseDao;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.OverflowList;
import com.atguigu.jxc.entity.OverflowListGoods;
import com.atguigu.jxc.service.OverflowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author zhmwi
 * @Date 2021/9/5
 * @Dersciption
 */
@Service
public class OverflowServiceImpl implements OverflowService {

    @Autowired
    private OverFlowDao overFlowDao;
    @Autowired
    private PurchaseDao purchaseDao;

    //保存到报溢单 保存到商品报溢单
    @Override
    public Boolean save(OverflowList overflowList,  List<OverflowListGoods> overflowGoodsList) {
        //保存到报溢单
        if (overflowList != null){
            overFlowDao.saveOverFlowList(overflowList);
        }

        //保存到商品报溢单
        List<Goods> goodsList = purchaseDao.selectGoodsAll();
        //根据商品编号分组
        Map<String, List<Goods>> goodsListMap =
                goodsList.stream().collect(Collectors.groupingBy(Goods::getGoodsCode));

        for (OverflowListGoods overflowGoods : overflowGoodsList) {
            String goodsCode = overflowGoods.getGoodsCode();
            if (goodsListMap.containsKey(goodsCode)){
                overflowGoods.setGoodsId(goodsListMap.get(goodsCode).get(0).getGoodsId());
                overflowGoods.setTotal(overflowGoods.getGoodsNum() * overflowGoods.getPrice());
                overflowGoods.setOverflowListId(overflowList.getOverflowListId());
                overFlowDao.saveOverflowListGoods(overflowGoods);
            }

        }

        return true;
    }

    //报溢单查询
    @Override
    public List<OverflowList> list(String sTime, String eTime) {
        return overFlowDao.list(sTime,eTime);
    }

    //查询报溢单商品
    @Override
    public List<OverflowList> goodsList(Integer overflowListId) {
        return overFlowDao.goodsList(overflowListId);
    }
}
